package com.zhao.concreteSubject;

import com.zhao.observer.Observer;
import com.zhao.subject.Subject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Subscription implements Subject {

    private List<Observer> observers = new ArrayList<>();

    @Override
    public void attach(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void detach(Integer index) {
        observers.remove(index);
    }

    @Override
    public void notifyAllObserver(String message) {
        Iterator<Observer> iterator = observers.iterator();
        while(iterator.hasNext()) {
            Observer observer = iterator.next();
            observer.update(message);
        }
    }
}
